#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.module.permission.service.impl;

import ${package}.basic.module.permission.mapper.PermissionMapper;
import ${package}.basic.module.permission.service.PermissionService;
import ${package}.common.bean.po.Element;
import ${package}.common.bean.po.Menu;
import ${package}.common.bean.po.Operate;
import ${package}.common.bean.po.Role;
import ${package}.common.emum.ElementStatusEnum;
import ${package}.common.emum.MenuStatusEnum;
import ${package}.common.emum.OperateStatusEnum;
import ${package}.common.emum.PermissionTypeEnum;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Description:
 *  校验权限
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Service
@AllArgsConstructor
public class PermissionServiceImpl implements PermissionService {

    /**
     * 权限Mapper
     */
    PermissionMapper permissionMapper;

    /**
     * Description:
     * <权限校验>
     * @author wupanhua
     * @date 10:28 2019/8/7
     * @param uri 1
     * @param roles 2
     * @return boolean
     **/
    @Override
    public boolean canIdoThis(String uri, List<Role> roles){

        List<Menu> m = permissionMapper.findMenuByRoleKeyAndPermissionType(roles, PermissionTypeEnum.MENU.getCode());
        if (CollectionUtils.isEmpty(m)) {
            return false;
        }
        for (Menu menu: m) {
            if ((!StringUtils.isEmpty(menu.getUrl())) && uri.contains(menu.getUrl()) && MenuStatusEnum.ENABLED.getCode().intValue() == menu.getStatus()) {
                return true;
            }
        }
        List<Element> e = permissionMapper.findElementByRoleKeyAndPermissionType(roles, PermissionTypeEnum.ELEMENT.getCode());
        for (Element element: e) {
            if ((!StringUtils.isEmpty(element.getUrl())) && uri.contains(element.getUrl()) && ElementStatusEnum.ENABLED.getCode().intValue() == element.getStatus()) {
                return true;
            }
        }
        List<Operate> o = permissionMapper.findOperateByRoleKeyAndPermissionType(roles, PermissionTypeEnum.OPERATE.getCode());
        for (Operate operate: o) {
            if ((!StringUtils.isEmpty(operate.getUrl())) && uri.contains(operate.getUrl()) && OperateStatusEnum.ENABLED.getCode().intValue() == operate.getStatus().intValue()) {
                return true;
            }
        }

        return false;
    }

    /**
     * Description:
     * <获取传入角色下所有的链接权限>
     * @author wupanhua
     * @date 11:46 2019-09-02
     * @param roles 角色列表
     * @return java.util.List<java.lang.String>
     */
    @Override
    public List<String> queryRolePermissionUri(List<Role> roles) {

        List<Menu> m = permissionMapper.findMenuByRoleKeyAndPermissionType(roles, PermissionTypeEnum.MENU.getCode());
        List<Element> e = permissionMapper.findElementByRoleKeyAndPermissionType(roles, PermissionTypeEnum.ELEMENT.getCode());
        List<Operate> o = permissionMapper.findOperateByRoleKeyAndPermissionType(roles, PermissionTypeEnum.OPERATE.getCode());

        return buildUrlList(m, e, o);
    }

    /**
     * Description:
     * <构建uri列表>
     * @author wupanhua
     * @date 11:46 2019-09-02
     * @param e 页面元素
     * @param m 菜单
     * @param o 操作
     * @return java.util.List<java.lang.String>
     */
    private List<String> buildUrlList(List<Menu> m, List<Element> e, List<Operate> o) {

        List<String> uri = Lists.newArrayList();

        m.forEach(menu -> uri.add(menu.getUrl()));
        e.forEach(element -> uri.add(element.getUrl()));
        o.forEach(operate -> uri.add(operate.getUrl()));

        return uri;
    }

}
